package cn.wei.youvie.service.impl;

import cn.wei.youvie.common.exception.BusinessException;
import cn.wei.youvie.common.servlet.ErrorCode;
import cn.wei.youvie.domain.dto.menu.MenuAddRequest;
import cn.wei.youvie.domain.dto.menu.MenuQueryRequest;
import cn.wei.youvie.domain.dto.menu.MenuUpdateRequest;
import cn.wei.youvie.domain.vo.MenuVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.wei.youvie.domain.entity.Menu;
import cn.wei.youvie.service.MenuService;
import cn.wei.youvie.mapper.MenuMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Youvie
* @description 针对表【menus(菜单表)】的数据库操作Service实现
* @createDate 2024-09-03 10:55:20
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
    implements MenuService{

    private final UserServiceImpl userServiceImpl;

    public MenuServiceImpl(UserServiceImpl userServiceImpl) {
        this.userServiceImpl = userServiceImpl;
    }

    @Override
    public Integer addMenu(MenuAddRequest menuAddRequest) {
        if (menuAddRequest.getName() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "菜单名称为空！");
        }
        if (menuAddRequest.getDescription() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "菜单描述为空！");
        }
        if (menuAddRequest.getSeque() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "菜单排序为空！");
        }
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuAddRequest, menu);
        boolean saved = this.save(menu);
        if (!saved) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return menu.getMenuId();
    }

    @Override
    public boolean update(MenuUpdateRequest menuUpdateRequest) {
        Menu menu = new Menu();
        BeanUtils.copyProperties(menuUpdateRequest, menu);
        boolean updated = this.updateById(menu);
        if (!updated) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return true;
    }

    @Override
    public List<MenuVO> getMenus(MenuQueryRequest menuQueryRequest) {
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();

        if (menuQueryRequest.getParentMenuId() != null) {
            menuQueryWrapper.eq("parent_menu_id", menuQueryRequest.getParentMenuId());
        }
        if (menuQueryRequest.getDescription() != null) {
            menuQueryWrapper.like("description", menuQueryRequest.getDescription());
        }
        if (menuQueryRequest.getRoles() != null) {
            menuQueryWrapper.like("roles", menuQueryRequest.getDescription());
        }
        if (menuQueryRequest.getRoutePath() != null) {
            menuQueryWrapper.like("url", menuQueryRequest.getRoutePath());
        }
        if (menuQueryRequest.getUsers() != null) {
            menuQueryWrapper.like("users", menuQueryRequest.getUsers());
        }

        Page<Menu> menus = this.page(new Page<>(menuQueryRequest.getCurrent(), menuQueryRequest.getPageSize()), menuQueryWrapper);
        return getMenuVOs(menus.getRecords());
    }

    @Override
    public List<MenuVO> getAllMenus(HttpServletRequest request) {
        QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
        menuQueryWrapper.like("users", userServiceImpl.getLoginUser(request).getId());
        menuQueryWrapper.orderByAsc("menu_id");
        Page<Menu> menus = this.page(new Page<>(1, 100), menuQueryWrapper);
        return getMenuVOs(menus.getRecords());
    }

    public List<MenuVO> getMenuVOs(List<Menu> menus) {
        return menus.stream().map(MenuVO::getMenuVO).collect(Collectors.toCollection(ArrayList::new));
    }
}
